<?php

namespace app\controller;

use app\services\AlipayService;
use plugin\admin\app\model\Good;
use plugin\admin\app\model\Order;
use support\Request;
use support\Response;

class AlipayController
{

    public function pay(Request $request)
    {
        $params = $request->post();
        $order = Order::where('sn',$params['order_sn'])->first();
        if ($order->is_pay == 1){
            return json(['code'=>1,'msg'=>'订单已支付']);
        }
        $goods = Good::where('id',$order->goods_id)->first();
        if (empty($goods)){
            return json(['code'=>1,'msg'=>'商品不存在']);
        }
        $params['goods_unique_id'] = $goods->unique_id;
        $pay = AlipayService::pay($params,$order)->getBody()->getContents();
        dump($pay);
        return json(['code'=>0,'pay' => $pay]);

    }

    public function notify(Request $request)
    {
        dump($request->post());
        AlipayService::notify($request->post());
        return new Response(200, [], 'success');
    }

}